{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Graphics."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "import matplotlib.dates as mdates\r\n",
    "import matplotlib.pyplot as plt\r\n",
    "import pandas as pd\r\n",
    "from numpy import (\r\n",
    "    array,\r\n",
    "    column_stack,\r\n",
    "    dot,\r\n",
    "    hstack,\r\n",
    "    logical_and,\r\n",
    "    mean,\r\n",
    "    ones,\r\n",
    "    sqrt,\r\n",
    "    squeeze,\r\n",
    "    var,\r\n",
    "    zeros,\r\n",
    ")\r\n",
    "from numpy.linalg import lstsq\r\n",
    "\r\n",
    "# Loading and transforming the data\r\n",
    "sp500_data = pd.read_csv(\"sp500.csv\", index_col=\"Date\", parse_dates=True)\r\n",
    "ftse_data = pd.read_csv(\"ftse.csv\", index_col=\"Date\", parse_dates=True)\r\n",
    "sp500 = sp500_data[\"Adj Close\"]\r\n",
    "sp500_dates = sp500_data.index\r\n",
    "ftse = ftse_data[\"Adj Close\"]\r\n",
    "ftse_dates = ftse_data.index"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 1\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "fig = plt.figure()\r\n",
    "ax = fig.add_subplot(111)\r\n",
    "ax.plot(sp500_dates, sp500)\r\n",
    "fmt = mdates.DateFormatter(\"%Y\")\r\n",
    "ax.xaxis.set_major_formatter(fmt)\r\n",
    "fig.autofmt_xdate()\r\n",
    "plt.draw()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 2\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "sp500_rets = sp500.resample(\"W-FRI\").last().pct_change().dropna()\n",
    "ftse_rets = ftse.resample(\"W-FRI\").last().pct_change().dropna()\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.hist(sp500_rets)\n",
    "plt.draw()\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.hist(sp500_rets, bins=20)\n",
    "plt.draw()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 3\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "bands = [(-1.0, -0.02), (-0.02, 0.0), (0.0, 0.02), (0.02, 1.0)]\n",
    "percs = zeros(len(bands))\n",
    "i = 0\n",
    "for b in bands:\n",
    "    percs[i] = mean(logical_and(sp500_rets > b[0], sp500_rets <= b[1]))\n",
    "    i += 1\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "labels = [\"<-2%\", \">-2% and <0\", \">0 and <2%\", \">2%\"]\n",
    "ax.pie(percs, labels=labels)\n",
    "plt.draw()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 4\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.scatter(sp500_rets, ftse_rets)\n",
    "ax.set_xlabel(\"S&P 500 returns\")\n",
    "ax.set_ylabel(\"ftse 100 returns\")\n",
    "plt.draw()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 5\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x = column_stack((ones(sp500_rets.shape), sp500_rets))\n",
    "y = column_stack((ftse_rets,))\n",
    "x, y\n",
    "out = lstsq(x, y, rcond=None)\n",
    "b = out[0]\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.scatter(sp500_rets, ftse_rets)\n",
    "ax.set_xlabel(\"S&P 500 returns\")\n",
    "ax.set_ylabel(\"FTSE 100 returns\")\n",
    "x = ax.get_xlim()\n",
    "x = array(x)\n",
    "x.shape = -1, 1\n",
    "x = hstack((ones(x.shape), x))\n",
    "fit = dot(x, b)\n",
    "b = squeeze(b)\n",
    "ax.plot(x[:, 1], fit, color=\"#800000\")\n",
    "ax.text(0, -0.15, f\"y = {b[0]:0.2f} + {b[1]:0.2f}x\")\n",
    "plt.draw()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 6\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "T = sp500_rets.size\n",
    "ewma = zeros((T, 2))\n",
    "r = column_stack((sp500_rets, ftse_rets))\n",
    "ewma[0] = var(r, axis=0)\n",
    "for i in range(1, T):\n",
    "    ewma[i] = 0.97 * ewma[i - 1] + 0.03 * r[i - 1] ** 2\n",
    "ewma = 100 * sqrt(252 * ewma)\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "ax.plot(sp500_rets.index, ewma[:, 0], label=\"S&P 500 ewma Vol (Annualized)\")\n",
    "ax.plot(ftse_rets.index, ewma[:, 1], label=\"ftse 100 ewma Vol (Annualized)\")\n",
    "ax.legend(loc=0)\n",
    "ax.set_title(\"Annualized Volatility (%)\")\n",
    "fmt = mdates.DateFormatter(\"%Y\")\n",
    "ax.xaxis.set_major_formatter(fmt)\n",
    "fig.autofmt_xdate()\n",
    "plt.show()"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}